from app.models import CustomerMessages
from app import db
from exts import JsonConverter, BeanUtils


def customer_messages_add(customerMessages):
    '''
    添加客户留言
    :param news:
    :return:
    '''
    o = JsonConverter.dict_to_object(customerMessages, CustomerMessages)
    db.session.add(o)
    db.session.commit()


def customer_messages_update(customerMessages):
    '''
    更新客户留言
    :param news:
    :return:
    '''
    customer = CustomerMessages.query.get(customerMessages['id'])
    BeanUtils.copy(customerMessages, customer)
    db.session.commit()


def customer_messages_delete(id):
    '''
    删除客户留言
    :param id:
    :return:
    '''
    customer = CustomerMessages.query.get(id)
    db.session.delete(customer)
    db.session.commit()


def customer_messages_get_by_id(id):
    return CustomerMessages.query.get(id)


def customer_messages_page(pageNum, pageSize):
    '''
    分页查询客户留言
    :return:
    '''
    return CustomerMessages.query.paginate(page=pageNum, per_page=pageSize)
